/**
 * Copyright (c) 2011 ~ 2014 Deepin, Inc.
 *               2013 ~ 2014 jouyouyun
 *
 * Author:      jouyouyun <jouyouwen717@gmail.com>
 * Maintainer:  jouyouyun <jouyouwen717@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 **/

package main

import (
	"fmt"
	"io/ioutil"
	"regexp"
	"strings"
)

func isElementExist(ele string, arrays []string) (bool, string) {
	key := "^" + strings.Split(ele, ":")[0]
	for _, v := range arrays {
		if ok, _ := regexp.MatchString(key, v); ok {
			//if strings.Contains(v, ele) {
			return true, v
		}
	}

	return false, ""
}

func getLocaleList() []string {
	contents, _ := ioutil.ReadFile("locale")
	lines := strings.Split(string(contents), "\n")

	return lines
}

func deleteSpaceChar(str string) string {
	if len(str) < 1 {
		return ""
	}

	ret := ""
	//fmt.Printf("\nbefore delete:%s---\n", str)
	for _, ch := range str {
		if ch == ' ' || ch == '\t' {
			continue
		}
		ret += string(ch)
	}
	//fmt.Printf("after delete:%s---\n", ret)

	return ret
}

func main() {
	list := getLocaleList()
	contents, _ := ioutil.ReadFile("locale-en")
	lines := strings.Split(string(contents), "\n")
	fmt.Println("package main\n")
	fmt.Println("var (")
	fmt.Println("\tlocalDescMap = map[string]string {")
	for _, line := range lines {
		if len(line) < 1 {
			continue
		}
		strs := strings.Split(line, " ")
		if ok, v := isElementExist(strs[0], list); ok {
			fmt.Printf("\t\t\"%s\": dlib.Tr(\"", v)
			l := len(strs)
			for j := 1; j < l; j++ {
				ret := deleteSpaceChar(strs[j])
				fmt.Printf("%s", ret)
				if j != l-1 {
					fmt.Printf(" ")
				}
			}
			fmt.Printf("\"),\n")
		}
	}
	fmt.Printf("\t}\n)\n")
}
